package com.organization

import grails.test.*

class TreeServiceTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testServiceMethods() {
        //create testing service
        def testService = new TreeService()

        //mock commonService methods
        def mockControl = mockFor(CommonService,true)
        mockControl.demand.nodeIdToDbId(2..2) {nodeId->
            nodeId.replaceAll("[a-zA-Z/_]","")
        }
        testService.commonService = mockControl.createMock()

        //mock domain for tests
        mockDomain(Department)
        def dep1 = new Department(id: 1, name:"Dep1").save()
        def dep2 = new Department(id: 2, name:"Dep2").save()
        def dep3 = new Department(id: 3, name:"Dep3").save()
        def proj1 = new Project(id: 1, department: dep1, name: "Proj1", startDate: new Date())
        def proj2 = new Project(id: 2, department: dep1, name: "Proj2", startDate: new Date())
        def proj3 = new Project(id: 3, department: dep2, name: "Proj3", startDate: new Date())
        mockDomain(Project,[proj1,proj2,proj3])
        def empl1 = new Employee(id: 1, project: proj1, firstName: "Ivan", lastName: "Ivanov", hireDate: null, birthDate: null)
        def empl2 = new Employee(id: 2, project: proj1, firstName: "Petr", lastName: "Petrov", hireDate: null, birthDate: null)
        def empl3 = new Employee(id: 3, project: proj2, firstName: "Fedor", lastName: "Fedorov", hireDate: null, birthDate: null)
        def empl4 = new Employee(id: 4, project: proj2, firstName: "Stepan", lastName: "Stepanov", hireDate: null, birthDate: null)
        mockDomain(Employee,[empl1,empl2,empl3,empl4])

        //test getEmployeeTreeByProj(def projId)
        def result = testService.getEmployeeTreeByProj(1)
        assertNotNull "null result in getEmployeeTreeByProj(projId)",result
        assertEquals 2,result.size()
        assertEquals "Petrov Petr",result[1].text
        assertEquals "true",result[1].leaf
        assertEquals "Employee_2",result[1].id

        result = testService.getEmployeeTreeByProj(null)
        assertNotNull "null result in getEmployeeTreeByProj(projId)",result
        assertEquals 0,result.size()

        //test getProjectsTreeByProj(def projId)
        result = testService.getProjectsTreeByDep(1)
        assertNotNull "null result in getProjectsTreeByDep(depId)",result
        assertEquals 2,result.size()
        assertEquals "Proj2",result[1].text
        assertEquals "Project_2",result[1].id
        assertEquals 2,result.children.size()

        result = testService.getProjectsTreeByDep(null)
        assertNotNull "null result in getProjectsTreeByDep(depId)",result
        assertEquals 0,result.size()

        //test getTree()
        result = testService.getTree()
        assertNotNull "null result in getTree()",result
        assertEquals 3,result.size()
        assertEquals "Dep2",result[1].text
        assertEquals "Department_2",result[1].id
        assertEquals 1,result[1].children.size()

        //test getBranch(nodeId)
        assertNotNull "null branch returned for null node",testService.getBranch(null)
        assertEquals 0,testService.getBranch(null).size()

        result = testService.getBranch(testService.commonService.rootNodeStr)
        assertNotNull "null result in getBranch(nodeId)",result
        assertEquals 3,result.size()
        assertEquals "Dep2",result[1].text

        result = testService.getBranch(testService.commonService.depNodeStr+"2")
        assertNotNull "null result in getBranch(nodeId))",result
        assertEquals 1,result.size()
        assertEquals "Proj3",result[0].text

        result = testService.getBranch(testService.commonService.projNodeStr+"1")
        assertNotNull "null result in getBranch(nodeId))",result
        assertEquals 2,result.size()
        assertEquals "Ivanov Ivan",result[0].text

        //verify count of mocked methods calls
        mockControl.verify()
    }
}